﻿using RoadQualityEvaluatorData.Domain.Entities;
using RoadQualityEvaluatorData.DomainServices.Generic;
using RoadQualityEvaluatorData.DomainServices.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RoadQualityEvalAPI.Helpers
{
    public class SpeedbumpHelper
    {
        private SpeedbumpRepository _speedbumpRepo;
        private MapSectionRepository _mapSectionRepo;

        public SpeedbumpRepository SpeedbumpRepo
        {
            get
            {
                return this._speedbumpRepo;
            }
        }

        public MapSectionRepository MapSectionRepo
        {
            get
            {
                return this._mapSectionRepo;
            }
        }

        public SpeedbumpHelper()
        {
            _speedbumpRepo = new SpeedbumpRepository();
            _mapSectionRepo = new MapSectionRepository();
        }

        public SpeedbumpHelper(UnitOfWork uo)
        {
            _speedbumpRepo = new SpeedbumpRepository(uo);
            _mapSectionRepo = new MapSectionRepository(uo);
        }


        public Speedbump CheckIfSpeedbumpAlreadyExists(Speedbump speedbump)
        {
            double minimumDistance = 1000;
            Speedbump closestExistingSpeedbump = null;
            foreach (var sb in _speedbumpRepo.GetAll())
            {
                var distanceBetweenPotholes = GeneralHelpers.ComputeDistanceBetweenTwoLocationPoints(sb.Location.Latitude, sb.Location.Longitude, speedbump.Location.Latitude, speedbump.Location.Longitude);

                if (distanceBetweenPotholes < minimumDistance)
                {
                    minimumDistance = distanceBetweenPotholes;
                    closestExistingSpeedbump = sb;
                }
            }

            if (minimumDistance <= 9)
                return closestExistingSpeedbump;
            else
                return null;
        }


        public void AssignSpeedbumpToMapSection(Speedbump sb)
        {
            double minimumDistance = 1000;
            MapSection closestMapSectionToSpeedbump = new MapSection();

            foreach (var mapSection in _mapSectionRepo.GetAll())
            {
                GeneralHelpers.Point mapSectionProjectionPoint;
                var distanceFromSpeedbumpToMapSection_Rough = GeneralHelpers.GetDistance(new GeneralHelpers.Point(sb.Location.Latitude, sb.Location.Longitude), new GeneralHelpers.Point(mapSection.StartLocation.Latitude, mapSection.StartLocation.Longitude), new GeneralHelpers.Point(mapSection.EndLocation.Latitude, mapSection.EndLocation.Longitude), out mapSectionProjectionPoint);
                var distanceFromSpeedbumpToProjectionPoint = GeneralHelpers.ComputeDistanceBetweenTwoLocationPoints(sb.Location.Latitude, sb.Location.Longitude, mapSectionProjectionPoint.X, mapSectionProjectionPoint.Y);

                if (distanceFromSpeedbumpToProjectionPoint < minimumDistance)
                {
                    minimumDistance = distanceFromSpeedbumpToProjectionPoint;
                    closestMapSectionToSpeedbump = mapSection;
                }
            }

            if (minimumDistance <= 100)
            {
                closestMapSectionToSpeedbump.Speedbumps.Add(sb);
                _mapSectionRepo.Update(closestMapSectionToSpeedbump);
            }
        }


    }
}